package com.scoressystem.controller;

import com.scoressystem.pojo.Student;
import com.scoressystem.service.StudentService;
import com.scoressystem.utils.Results;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    StudentService studentService;
    @RequestMapping("/getAllStudent")
    @ResponseBody
    public Results<List<Student>> getAllStudentPage(@RequestParam(value="stuName",defaultValue = "")String stuName,
                                                    @RequestParam(value = "page",defaultValue = "1") int pageIndex,
                                                    @RequestParam(value = "limit",defaultValue = "5") int pageSize) {
        Map<String,Object> map = new HashMap<>();
        map.put("stuName", "%"+stuName+"%");
        map.put("startPage", (pageIndex-1)*pageSize);
        map.put("pageSize", pageSize);
        List<Student> students=studentService.SelectAllStudentBypage(map);
        long total=studentService.SelectAllStudentCount(map);
        Results<List<Student>> results=new Results("0","查询成功",students,total);
        return results;

    }
    @RequestMapping("/addStudent")
    @ResponseBody
    public Results addStudent(Student student) {

        int count1 = studentService.searchAllByStuNoInt(student);
        if (count1 > 0) {
            Results results = new Results("1", "新增失败，学号已存在");
            return results;

        } else {
            studentService.insertStudent(student);
            Results results = new Results("0", "新增成功");
            return results;
        }
    }
    @RequestMapping("/updateStudent")
    @ResponseBody
    public Results updateStudent(Student student) {
        int count1 = studentService.updateStudent(student);
        if (count1 > 0) {
            Results results=new Results("0","更新成功");
            return results;
        }
        else {
            Results results=new Results("1","更新失败");
            return results;

        }
    }
    @RequestMapping("/deleteStudent")
    @ResponseBody
    public Results deleteStudent(Student student) {
      int count=  studentService.deleteByStuNoInt(student);
      if (count > 0) {
          Results results=new Results("0","删除成功");
          return results;
      }
      else {
          Results results=new Results("1","删除失败");
          return results;

      }

    }
    @RequestMapping("/studentSelect")
    @ResponseBody
    public Results studentSelect(Student student) {
     List<Student> students=  studentService.selectAllStudentBydel();
     return new Results("0","查询成功",students);

    }
    @RequestMapping("/login")
    @ResponseBody //返回值自动转json
    public Results login( @Param("stuNo") String userName,
                          @Param("stuPass")  String password,
                          String userCode, HttpSession session) {
        String code=(String) session.getAttribute("systemCode");
        if(code.equalsIgnoreCase(userCode)) {
            Student student=  studentService.selectStudentlogin(userName,password);
            if(student!=null) {
                session.setAttribute("uname",student.getStuName());
                session.setAttribute("stuNo",student.getStuNo() );
                Results results = new Results("0","登录成功");
                return results;
            }
            else {
                Results results = new Results("1","登录失败");
                return results;
            }
        }
        else {
            Results results = new Results("3","验证码错误");
            return results;
        }
    }
    @RequestMapping("/updatePwd")
    @ResponseBody //返回值自动转json
    public Results updatePwd(String pwd ,String pwd1,String pwd2,HttpSession session) {
        String stuNo= String.valueOf(session.getAttribute("stuNo"));
        Student student= studentService.selectStudentlogin(stuNo,pwd);
        if(student!=null) {
            if (pwd1.equalsIgnoreCase(pwd2)){
                Integer count1=  studentService.updateStudentpass( pwd1,stuNo);
                if (count1!=null){
                    session.invalidate();
                    return  new  Results("0","修改成功");
                }
                else {
                    return  new  Results("3","修改失败");
                }
            }
            else {
                return new  Results("2","两次密码不相同");
            }
        }
        else {
            return  new Results("1","账户密码错误");
        }

    }

    }

